<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增用户')" />
</head>
<body>

<div id="app" v-cloak>
    <div class="main">
        <el-form :inline="true" :model="searchForm">
            <el-form-item>
                <el-button type="primary" @click="add_user">新增管理员</el-button>
            </el-form-item>
            <el-form-item label="管理员名称">
                <el-input v-model="searchForm.user_name" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="归属角色">
                <el-select v-model="searchForm.role_id" placeholder="归属角色">
                    <el-option label="全部" value=""></el-option>
                    <el-option :label="item.role_name" :value="item.id" v-for="(item,index) in role_list" :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否锁定">
                <el-select v-model="searchForm.status" placeholder="是否锁定">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="锁定中" value="1"></el-option>
                    <el-option label="正常" value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
                <el-button @click="resetForm">取消</el-button>
            </el-form-item>
        </el-form>

        <el-table :data="tableData" border v-loading="loading">
            <el-table-column fixed label="操作" width="150">
                <template slot-scope="scope">
                    <el-button plain  size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button plain  size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
            <el-table-column fixed label="头像" width="61">
                <template slot-scope="scope">
                    <el-image style="width: 40px; height: 40px" :src="getImgUrl(scope.row.pic)" v-if="scope.row.pic != '' "></el-image>
                    <el-image style="width: 40px; height: 40px" src="__PUB__/user_pic.jpg" v-if="scope.row.pic == '' "></el-image>
                </template>
            </el-table-column>
            <el-table-column fixed label="管理员" width="150">
                <template slot-scope="scope">
                     <span>{{ scope.row.user_name }}</span>
                </template>
            </el-table-column>
<!--            <el-table-column label="角色名称" width="250">-->
<!--                <template slot-scope="scope">-->
<!--                     <span>{{ scope.row.role_name }}</span>-->
<!--                </template>-->
<!--            </el-table-column>-->
            <el-table-column label="最后登录时间" width="180">
                <template slot-scope="scope">
                     <span v-if="scope.row.login_time">{{ scope.row.login_time | time }}</span>
                </template>
            </el-table-column>
            <el-table-column label="最后登录IP" width="180">
                <template slot-scope="scope">
                     <span>{{ scope.row.login_ip }}</span>
                </template>
            </el-table-column>

            <el-table-column label="注册时间" width="180">
                <template slot-scope="scope">
                     <span>{{ scope.row.add_time | time }}</span>
                </template>
            </el-table-column>
            <el-table-column label="登录次数" width="100">
                <template slot-scope="scope">
                     <span>{{ scope.row.login_num }}</span>
                </template>
            </el-table-column>
            <el-table-column label="状态" width="80">
                <template slot-scope="scope">
                    <el-tag type="success" size="mini" effect="dark" v-if="scope.row.status == 0">正常</el-tag>
                    <el-tag type="warning" size="mini" effect="dark" v-if="scope.row.status == 1">锁定</el-tag>
                </template>
            </el-table-column>
        </el-table>
        <div class="block">
            <el-pagination
                    background
                    @current-change="handleCurrentChange"
                    :current-page.sync="searchForm.page"
                    :page-size="searchForm.page_size"
                    layout="total, prev, pager, next"
                    :total="searchForm.total">
            </el-pagination>
        </div>
    </div>
</div>

<script>
    var vm = new Vue({
        el: '#app',
        data(){
            return {
                loading:false,
                tableData:'',
                searchForm:{
                    user_name:'',
                    role_id:'',
                    status:'',
                    page_size: 5,
                    total: 0,
                    page: 1
                },
                role_list:[(${role_list})],
            }
        },
        methods: {
            load_table(){
                this.loading = true;
                let that = this;
                axios.post('/system/get_user_list',this.searchForm).then(function (res){
                    that.loading = false;
                    let data = res.data;
                    that.tableData = data.data
                    that.searchForm.total = data.total;
                    console.log(that.searchForm);
                }).catch(function(error){
                    that.loading = false;
                    console.log(error);
                });
            },
            add_res(msg,type){
                if(type==null)
                {
                    type='success';
                }
                if(type=='success')
                {
                    this.load_table();
                }
                this.$message({
                    message: msg,
                    type: type
                });
            },
            add_user(){
                this.open_drawer_layer('增加角色','/system/add_user','50%');
            },
            handleEdit(index, row) {
                this.open_drawer_layer('编辑角色','/system/add_user?id='+row.id,'50%');
            },
            handleDelete(index, row) {
                let that = this;
                this.$confirm('操作不可逆，是否删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get('/system/delete_user?id='+row.id).then(function (res){
                        let data = res.data;
                        if(data.code==true)
                        {
                            that.$message({
                               message: '删除成功',
                               type: 'success'
                            });
                            that.load_table();
                        }else{
                            that.$message.error(data.msg);
                        }
                    }).catch(function(error){
                        console.log(error);
                    });
                }).catch(() => {

                });
            },
            getImgUrl(url){
                return url;
            },
            onSubmit() {
                this.searchForm.page = 1;
                this.load_table();
            },
            resetForm(){
                this.searchForm = {
                    user_name:'',
                    role_id:'',
                    status:'',
                    page:1,
                    page_size:5
                };
                this.load_table();
            },
            handleCurrentChange(val) {
                this.searchForm.page = val;
                this.load_table();
            },
        },
        created() {
            this.load_table();
        },
    })
</script>


</body>
</html>